package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.model.response.MaterialCheckStatisticsVO;
import com.wing.cabinet.dao.CheckedResultDao;
import com.wing.cabinet.model.entity.CheckedResult;
import com.wing.cabinet.model.request.CheckedResultForm;
import com.wing.cabinet.model.response.CheckedResultVO;
import com.wing.cabinet.model.response.CheckedScrapVO;
import com.wing.cabinet.model.response.InspectionAndRepairExportVO;
import com.wing.common.utils.JsonResult;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.DepartmentVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 盘验结果表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CheckedResultServiceImpl extends ServiceImpl<CheckedResultDao, CheckedResult> implements CheckedResultService {

    @Autowired
    private UserClient userClient;
    @Override
    public int add(CheckedResultForm checkedResultForm) {

        //校验

        CheckedResult checkedResult =new CheckedResult();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CheckedResult.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(checkedResultForm, checkedResult, copyOptions);

        return baseMapper.insert(checkedResult);
    }

    @Override
    public List<CheckedResultVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CheckedResultVO> pageList4app(Page page, Map<String, Object> params) {
        List<CheckedResultVO> list = baseMapper.list4app(page, params);
        for(CheckedResultVO item : list){
            JsonResult<DepartmentVO> jsonResult = userClient.getDepartmentVOById(item.getDepartmentId());
            if(null != jsonResult && null != jsonResult.getData()) {
                item.setCostCenter(jsonResult.getData().getCostCenter());
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public CheckedResultVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int updateIsGenOrder(List<Long> idList) {
        return baseMapper.updateIsGenOrder(idList);
    }

    @Override
    public List<CheckedResultVO> exportList(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public List<InspectionAndRepairExportVO> getInspectionAndRepairExportVOList(Map<String, Object> params) {
        return baseMapper.getInspectionAndRepairExportVOList(params);
    }

    @Override
    public List<CheckedScrapVO> getCheckedScrapVoExportList(Map<String, Object> params) {
        return baseMapper.getCheckedScrapVoExportList(params);
    }

    @Override
    public Page<HashMap> getCabCodeGroupBy(Page page,String cabCode,String belongCabinet) {

        List<HashMap> list = baseMapper.getCabCodeGroupBy(page,cabCode,belongCabinet);

        Iterator<HashMap> iterator = list.iterator();
        while (iterator.hasNext()) {
            HashMap map = iterator.next();
            List<HashMap> list2 = baseMapper.getStatisticsByCabCode(map.get("cabCode").toString());
            if (CollectionUtils.isNotEmpty(list2)) {
                map.put("statisticsList", list2);
            }else {
                iterator.remove();
            }
        }

        page.setRecords(list);
        return page;
    }

//    @Override
//    public List<HashMap> getStatisticsByCabCode(String cabCode) {
//        return baseMapper.getStatisticsByCabCode(cabCode);
//    }

    @Override
    public List<HashMap> getDetailStatisticsByCabCode(String cabCode, Integer spaceCode,Integer businessType) {
        return baseMapper.getDetailStatisticsByCabCode(cabCode,spaceCode,businessType);
    }

    @Override
    public Page<HashMap> getTaskGroupBy(Page page,String taskCode,String cabCode) {
        List<HashMap> list = baseMapper.getTaskGroupBy(page,taskCode,cabCode);
        page.setRecords(list);
        return page;
    }

    @Override
    public JsonResult topLineChart(Map<String, Object> params, Integer days) {
        HashMap hm = new HashMap();
        hm.put("title", "物料盘验统计（按天）"); // 固定统计10天的数据
        List<HashMap> mapList = baseMapper.topLineChart(params);
        // 获取10天的集合
        List<String> tenDaysList = ReportUtil.getTenDaysList(days);
        List<HashMap> list = new ArrayList<>();
        // 给没有数据的那些天补充数据
        for (String dayStr : tenDaysList) {
            boolean isHave = false;
            for (HashMap map : mapList) {
                if(map.get("time").equals(dayStr)){
                    isHave = true;
                    list.add(map);
                }
            }
            if(!isHave){
                HashMap map = new HashMap();
                String year = dayStr.split("-")[0];
                String month = dayStr.split("-")[1];
                String day = dayStr.substring(dayStr.lastIndexOf("-")+1);
                map.put("name", month+"/"+day);
                map.put("value", 0);
                list.add(map);
            }
        }

        for (HashMap map : list) {
            map.remove("time");
        }
        hm.put("data", list);
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult<MaterialCheckStatisticsVO> materialCheckStatistics(Map<String, Object> params) {
        MaterialCheckStatisticsVO materialCheckStatisticsVO = baseMapper.materialCheckStatistics(params);
        return JsonResult.success(materialCheckStatisticsVO);
    }

    @Override
    public JsonResult materialCheckResult(Map<String, Object> params) {
        HashMap hm = new HashMap();
        hm.put("title", "物料盘验结果（按物料）");
        List<HashMap> mapList = baseMapper.materialCheckResult(params);
        List<String> titleList = new ArrayList<>();
        titleList.add("暂存");
        titleList.add("报修");
        titleList.add("报废");
        titleList.add("规格不相符");
        titleList.add("缺失");
        hm.put("dataTitle", titleList);
        hm.put("data", mapList);
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult materialCheckRankingPieChart(Map<String, Object> params) {
        HashMap hm = new HashMap();
        hm.put("title", "物料盘验排名（按柜子）");
        List<HashMap> mapList = baseMapper.materialCheckRankingPieChart(params);
        hm.put("data", mapList);
        return JsonResult.success(hm);
    }


}
